// pages/home/home.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    page_index: 0,
    page_size: 12,
    load: true,
    video_class: 'KoreaJapan',
    video_class_name: '',
    video_type_list: '',
    video_type_list_count: '',
    video_type: '',
    video_list: []
  },

  /**
   * 生命周期函数--监听页面加载
   */

  //获取视频类型列表
  getVideoTypeList: function() {
    var that = this
    wx.request({
      url: 'http://navod.scse.com.cn/nn_cms/nn_cms_view/std/n3_a_xml.php',
      data: {
        nns_nxtype: 'd',
        nns_tag: 29,
        nns_user_id: '',
        nns_device_id: '',
        nns_func: 'get_media_assets_info',
        nns_media_assets_id: that.data.video_class,
        nns_user_id: '',
        nns_token: '',
        nns_output_type: 'json',
      },
      method: 'GET',
      success: function(res) {
        console.log('typeList=>')
        console.log(res)
        that.setData({
          video_type_list: res.data.data.category.category,
          video_type_list_count: res.data.data.category.category.length,
          video_type: res.data.data.category.category['0']['@attributes'].id,
          video_class_name: res.data.data["@attributes"].name
        })
        that.getVideoList()
        wx.setNavigationBarTitle({
          title: that.data.video_class_name
        })
      }
    })

  },
  //获取视频列表
  getVideoList: function() {
    var that = this
    wx.request({
      url: 'http://navod.scse.com.cn/nn_cms/nn_cms_view/std/n3_a_xml.php',
      data: {
        nns_nxtype: 'd',
        nns_tag: 29,
        nns_user_id: '',
        nns_device_id: '',
        nns_func: 'get_media_assets_item_list',
        nns_media_assets_id: that.data.video_class,
        nns_category_id: that.data.video_type,
        nns_page_index: that.data.page_index,
        nns_page_size: that.data.page_size,
        nns_sort_type: 'default',
        nns_include_category: 1,
        nns_user_id: '',
        nns_token: '',
        nns_output_type: 'json',
      },
      method: 'GET',
      success: function(res) {
        if (res.data.data.item != undefined) {
          var content = that.data.video_list.concat(res.data.data.item)
          //console.log('videoList=>')
          //console.log(res)
          that.setData({
            video_list: content,
            page_index: that.data.page_index + 1
          })
        }

      },
      fail: function(res) {
        that.setData({
          load: true
        })
        wx.showToast({
          title: '数据异常',
          icon: 'cancel',
          duration: 2000
        })
      },
      complete: function() {
        that.setData({
          load: true
        })
      }
    })
  },

  changeType: function(e) {
    console.log(e)
    this.setData({
      video_list: [],
      video_type: e.currentTarget.id,
      page_index: 0
    })
    this.getVideoList()
  },

  onLoad: function(options) {
    var that = this
    if (options.id != undefined) {
      this.setData({
        video_class: options.id
      })
    }
    this.getVideoTypeList()

  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {
    var that = this
    if (that.data.load) {
      that.setData({
        load: false
      })
      that.getVideoList()
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})